<template>
  <div class="wrapper">
    <!-- header部分 -->
    <header>
      <p>我的订单</p>
    </header>
    <!-- 订单列表部分 -->
    <div class="content">
      <h3 class="order-title">未支付订单信息：</h3>
      <div class="order-card-container">
        <template v-for="item in orderArr">
          <div v-if="item.orderState == 0" class="order-card">
            <div class="order-info">
              <p>
                {{ item.business.businessName }}
                <i class="fa fa-caret-down" @click="detailetShow(item)"></i>
              </p>
              <div class="order-info-right">
                <p>&#165;{{ item.orderTotal }}</p>
                <div class="order-info-right-icon" @click="goToPayment(item)">
                  去支付
                </div>
              </div>
            </div>

            <ul class="order-detailet" v-show="item.isShowDetailet">
              <li v-for="odItem in item.list">
                <p>{{ odItem.foodName }} x {{ odItem.quantity }}</p>
                <p>&#165;{{ odItem.foodPrice}}</p>
              </li>
              <li>
                <p>配送费</p>
                <p>&#165;{{ item.business.deliveryPrice }}</p>
              </li>
              <li>
                <p>订单配送至：</p>
                <p>{{ item.deliveryAddress }}</p>
              </li>
            </ul>
          </div>
        </template>
      </div>
      <h3 class="order-title">已支付订单信息：</h3>
      <div class="order-card-container">
        <template v-for="item in orderArr">
          <div v-if="item.orderState == 1" class="order-card">
            <div class="order-info">
              <p>
                {{ item.business.businessName }}
                <i class="fa fa-caret-down" @click="detailetShow(item)"></i>
              </p>
              <div class="order-info-right">
                <p>&#165;{{ item.orderTotal }}</p>
              </div>
            </div>

            <ul class="order-detailet" v-show="item.isShowDetailet">
              <li v-for="odItem in item.list">
                <p>{{ odItem.foodName }} x {{ odItem.quantity }}</p>
                <p>&#165;{{ odItem.foodPrice * odItem.quantity }}</p>
              </li>
              <li>
                <p>配送费</p>
                <p>&#165;{{ item.business.deliveryPrice }}</p>
              </li>
              <li>
                <p>订单配送至：</p>
                <p>{{ item.deliveryAddress }}</p>
              </li>
            </ul>
          </div>
        </template>
      </div>
    </div>
    <!-- 底部菜单部分 -->
    <Footer></Footer>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import Footer from '../components/Footer.vue';
import { useRouter } from 'vue-router';
import axios from 'axios';

const orderArr = ref([]);
const user = ref({});
const router = useRouter();

onMounted(async () => {
  user.value = JSON.parse(sessionStorage.getItem('user')) || {};
  await fetchOrders();
});

const detailetShow = (order) => {
  order.isShowDetailet = !order.isShowDetailet;
};

const fetchOrders = async () => {
  try {
    const response = await axios.get(`/orders/user/${user.value.userId}`);
    orderArr.value = response.data.data.map((order) => ({
      ...order,
      isShowDetailet: false,
    }));
  } catch (error) {
    console.error(error);
  }
};

const goToPayment = (item) => {
  router.push({ path: '/payment', query: { orderId: item.orderId } });
};
</script>

<style scoped>
.wrapper {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}
header {
  width: 100%;
  height: 12vw;
  background-color: #0097ff;
  color: #fff;
  font-size: 4.8vw;
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
}
.content {
  flex: 1;
  overflow-y: auto;
  margin-top: 12vw;
  padding-bottom: 16vw;
  padding: 4vw;
}
.order-card-container {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.order-title {
  width: 100%;
  padding: 1vw 0;
  background-color: #f0f0f0;
  border-left: 5px solid #0097ff;
  margin: 2vw 0;
  color: #333;
  font-size: 4vw;
  text-align: left;
  padding-left: 4vw;
  box-sizing: border-box;
}
.order-card {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  width: 90%;
  margin-bottom: 2vw;
  padding: 2vw;
  box-sizing: border-box;
}
.order-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1vw;
}
.order-info-right {
  display: flex;
}
.order-info-right-icon {
  background-color: #f90;
  color: #fff;
  border-radius: 3px;
  margin-left: 2vw;
  user-select: none;
  cursor: pointer;
}
.order-detailet {
  list-style: none;
  padding: 0;
  margin: 0;
}
.order-detailet li {
  padding: 1vw 0;
  font-size: 3vw;
  color: #666;
}
</style>
